#include "av_frame_buf.h"
#include "ffvdp_ring.h"

extern av_frame_buf_desc_t g_frame_buf_desc_generic;
av_frame_buf_desc_t *g_frame_buf_desc_array[AV_FRAME_BUF_TYPE_MAX + 1] = {
    &g_frame_buf_desc_generic, NULL,
};

av_frame_buf_manager_t *av_frame_buf_alloc_manager(av_frame_buf_type_t t) {
    av_frame_buf_desc_t *desc = g_frame_buf_desc_array[t];

    av_frame_buf_manager_t *m = NULL;

    if (desc->alloc_manager_func) {
        m = desc->alloc_manager_func(desc);
        if (m == NULL) {
            return NULL;
        }
    }

    return m;
}

av_frame_buf_t *av_frame_buf_alloc(av_frame_buf_manager_t *m) {
    return m->desc->alloc_func(m);
}

void av_frame_buf_free(av_frame_buf_t *buf) { buf->m->desc->free_func(buf); }

void av_frame_buf_reset(av_frame_buf_t *buf) { buf->m->desc->reset_func(buf); }

int av_frame_buf_add_data(av_frame_buf_t *buf, av_pkt_buf_t *pkt) {
    assert(buf->m->desc->add_data_func);
    return buf->m->desc->add_data_func(buf, pkt);
}

char *av_frame_buf_data(av_frame_buf_t *buf) {
    return buf->m->desc->data_func(buf);
}

size_t av_frame_buf_data_len(av_frame_buf_t *buf) {
    return buf->m->desc->data_len_func(buf);
}

av_frame_type_t av_frame_buf_type(av_frame_buf_t *buf) {
    return buf->m->desc->type_func(buf);
}

int av_frame_buf_is_keyframe(av_frame_buf_t *buf) {
    return buf->m->desc->is_keyframe_func(buf);
}
